package com.iot08.api.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;

/**
 * @version 1.0
 * @Description:
 * @time: 2021/5/12 14:09
 * @author: Rock
 */
public class XMLUtils {

    /**
     * xml字符串转对象
     *
     * @param clazz
     * @param xmlStr
     * @time: 2021/5/12 14:09
     * @author: Rock
     * @version 1.0
     */
    public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // 进行将Xml转成对象的核心接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;
    }


}
